package com.ruoyi.web.controller.business;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.EWallet;
import com.ruoyi.business.domain.qo.QueryObjectEDetail;
import com.ruoyi.business.service.IEWalletService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.EDetail;
import com.ruoyi.business.service.IEDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账户明细Controller
 *
 * @author hao
 * @date 2023-02-01
 */
@RestController
@RequestMapping("/business/detail")
public class EDetailController extends BaseController {
    @Autowired
    private IEDetailService eDetailService;
    @Autowired
    private IEWalletService ieWalletService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询账户明细列表
     */
    @PreAuthorize("@ss.hasPermi('business:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(QueryObjectEDetail eDetail) {
        startPage();
        List<EDetail> list = eDetailService.selectEDetailList(eDetail);
        return getDataTable(list);
    }

    /**
     * 导出账户明细列表
     */
    @PreAuthorize("@ss.hasPermi('business:detail:export')")
    @Log(title = "账户明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EDetail eDetail) {
        List<EDetail> list = eDetailService.selectEDetailList(eDetail);
        ExcelUtil<EDetail> util = new ExcelUtil<EDetail>(EDetail.class);
        util.exportExcel(response, list, "账户明细数据");
    }

    /**
     * 获取账户明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(eDetailService.getById(id));
    }

    /**
     * 新增账户明细
     */
    @PreAuthorize("@ss.hasPermi('business:detail:add')")
    @Log(title = "账户明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EDetail eDetail) {
        return toAjax(eDetailService.save(eDetail));
    }

    /**
     * 修改账户明细
     */
    @PreAuthorize("@ss.hasPermi('business:detail:edit')")
    @Log(title = "账户明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EDetail eDetail) {
        return toAjax(eDetailService.updateById(eDetail));
    }

    /**
     * 删除账户明细
     */
    @PreAuthorize("@ss.hasPermi('business:detail:remove')")
    @Log(title = "账户明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(eDetailService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 充值
     */
    @PostMapping("/congzhi")
    public AjaxResult congzhiAdd(@RequestBody EDetail eDetail) {
        return toAjax(eDetailService.congzhiByUserId(eDetail));
    }

    /**
     * 消费
     */
    @PostMapping("/xiaofei")
    public AjaxResult xiaofeiAdd(@RequestBody EDetail eDetail) {
        return toAjax(eDetailService.xiaofeiByUserId(eDetail));
    }

    /**
     * 当前用户列表展示
     */
    @GetMapping("/user/list")
    public AjaxResult userList() {
        ArrayList<SysUser> listDate = new ArrayList<>();
        List<EWallet> list = ieWalletService.list();
        for (EWallet eDetail : list) {
            SysUser byAllId = userService.getByAllId(eDetail.getUserId());
            listDate.add(byAllId);
        }
        return AjaxResult.success(listDate);
    }

}
